package com.innerex.inexclear.core.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * This class is used to represent available rule in the database.
 * 
 * @author <a href="mailto:evencia@innerex.com"> Edoardo Vencia
 *         (evencia@innerex.com)</a>
 */
@Entity
@Table(name = "clearingtransaction")
public class ClearingTransaction extends BaseObject implements Serializable {

	private static final long serialVersionUID = -4202223761971512783L;

	private Long id;
	private String product;
	private String market;
	private User buyerUser;
	private User sellerUser;
	private Integer quantity;
	private Integer price;
	private String priceType;
	private String currency;
	private String status;

	/**
	 * Default constructor - creates a new instance with no values set.
	 */
	public ClearingTransaction() {
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	@Column(length = 64)
	public String getProduct() {
		return this.product;
	}

	@Column(length = 64)
	public String getMarket() {
		return this.market;
	}

	@ManyToOne(targetEntity = User.class)
	public User getBuyerUser() {
		return buyerUser;
	}

	@ManyToOne(targetEntity = User.class)
	public User getSellerUser() {
		return sellerUser;
	}

	@Column
	public Integer getQuantity() {
		return quantity;
	}

	@Column
	public Integer getPrice() {
		return price;
	}

	@Column
	public String getPriceType() {
		return priceType;
	}

	@Column(length = 8)
	public String getCurrency() {
		return currency;
	}

	@Column(length = 64)
	public String getStatus() {
		return status;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public void setMarket(String market) {
		this.market = market;
	}

	public void setBuyerUser(User buyerUser) {
		this.buyerUser = buyerUser;
	}

	public void setSellerUser(User sellerUser) {
		this.sellerUser = sellerUser;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public void setPrice(Integer price) {
		this.price = price;
	}

	public void setPriceType(String priceType) {
		this.priceType = priceType;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * {@inheritDoc}
	 */
	public int compareTo(Object o) {
		return (equals(o) ? 0 : -1);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((buyerUser == null) ? 0 : buyerUser.hashCode());
		result = prime * result
				+ ((currency == null) ? 0 : currency.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((market == null) ? 0 : market.hashCode());
		result = prime * result + ((price == null) ? 0 : price.hashCode());
		result = prime * result
				+ ((priceType == null) ? 0 : priceType.hashCode());
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		result = prime * result
				+ ((quantity == null) ? 0 : quantity.hashCode());
		result = prime * result
				+ ((sellerUser == null) ? 0 : sellerUser.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
			return false;
		ClearingTransaction other = (ClearingTransaction) obj;
		if (buyerUser == null) {
			if (other.buyerUser != null)
				return false;
		} else if (!buyerUser.equals(other.buyerUser))
			return false;
		if (currency == null) {
			if (other.currency != null)
				return false;
		} else if (!currency.equals(other.currency))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (market == null) {
			if (other.market != null)
				return false;
		} else if (!market.equals(other.market))
			return false;
		if (price == null) {
			if (other.price != null)
				return false;
		} else if (!price.equals(other.price))
			return false;
		if (priceType == null) {
			if (other.priceType != null)
				return false;
		} else if (!priceType.equals(other.priceType))
			return false;
		if (product == null) {
			if (other.product != null)
				return false;
		} else if (!product.equals(other.product))
			return false;
		if (quantity == null) {
			if (other.quantity != null)
				return false;
		} else if (!quantity.equals(other.quantity))
			return false;
		if (sellerUser == null) {
			if (other.sellerUser != null)
				return false;
		} else if (!sellerUser.equals(other.sellerUser))
			return false;
		if (status == null) {
			if (other.status != null)
				return false;
		} else if (!status.equals(other.status))
			return false;
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this,
				ToStringStyle.DEFAULT_STYLE).append("id", this.id).append(
				"product", this.product).append("market", this.market).append(
				"  FIX ME: Add other fields");
		return sb.toString();
	}

}
